package org.zch.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.zch.common.ApiResult;
import org.zch.dao.OrdersDAO;
import org.zch.entity.dto.OrdersDTO;
import org.zch.entity.po.GoodsPO;
import org.zch.entity.po.OrdersPO;
import org.zch.entity.po.UserInfoPO;
import org.zch.entity.vo.OrderInsertVO;
import org.zch.entity.vo.OrdersVO;
import org.zch.entity.vo.UserInfoVO;
import org.zch.service.GoodsService;
import org.zch.service.OrdersService;
import org.springframework.stereotype.Service;
import org.zch.service.UserService;
import org.zch.common.utils.CodeNoUtil;

import java.util.Date;

/**
 * 订单表(Order)表服务实现类
 *
 * @author 刘新
 * @since 2024-01-10 11:19:40
 */
@Service("ordersService")
public class OrdersServiceImpl extends ServiceImpl<OrdersDAO, OrdersPO> implements OrdersService {

    @Autowired
    private OrdersDAO ordersDAO;

    @Autowired
    private CodeNoUtil codeNoUtil;

    @Autowired
    private GoodsService goodsService;
    @Autowired
    private UserService userService;

    /**
     * 生成订单
     * @param ordersPO
     * @return
     */
    @Override
    public OrderInsertVO insert(OrdersPO ordersPO) {
        OrderInsertVO orderInsertVO = new OrderInsertVO();
        orderInsertVO.setPutSuccess(true);
        // 校验是否能够兑换
        UserInfoVO infoVO = userService.getByCode(ordersPO.getUserCode());
        GoodsPO goodsPO = goodsService.getById(ordersPO.getGoodsId());
        if (null == goodsPO.getIntegral()){
            goodsPO.setIntegral(10000);
        }
        if (null == infoVO.getLaoMoney() || goodsPO.getIntegral() > infoVO.getLaoMoney()){
            // throw new RuntimeException("余额不足，无法进行兑换");
            orderInsertVO.setPutSuccess(false);
            orderInsertVO.setLaoMoney(infoVO.getLaoMoney());
            return orderInsertVO;
        }
        // 扣减余额
        userService.updateLaoMoney(ordersPO.getUserCode(), -goodsPO.getIntegral());
        // 生成订单编号
        String orderNo = codeNoUtil.getOrderNo();
        ordersPO.setOrderNo(orderNo);
        ordersPO.setCreateTime(new Date());
        //规定编码，用于立即兑换商品，如APP内皮肤
        if (ordersPO.getOrderStatus() != null && ordersPO.getOrderStatus() == 10086){
            //给用户添加皮肤
            UserInfoPO userInfoPO = new UserInfoPO();
            userInfoPO.setCode(ordersPO.getUserCode());
            String mS = "";
            String userSkin = infoVO.getSkin();
            if (userSkin != null && !userSkin.isEmpty()){
                mS = userSkin + "-" + goodsPO.getGoodsImg();
            }else {
                mS = goodsPO.getGoodsImg();
            }
            userInfoPO.setSkin(mS);
            userService.update(userInfoPO);
            //立即兑换商品状态直接完成
            ordersPO.setOrderStatus(2);
            ordersPO.setLogNo("无");
        }else {
            ordersPO.setOrderStatus(0);
        }
        int i = ordersDAO.insert(ordersPO);
        if (i != 1){
            orderInsertVO.setPutSuccess(false);
            orderInsertVO.setLaoMoney(-1);
        }
        // 商品兑换数量+1
        if (null == goodsPO.getRedeemedQuantity()){
            goodsPO.setRedeemedQuantity(0);
        }
        GoodsPO updateGoods = new GoodsPO();
        updateGoods.setId(goodsPO.getId());
        updateGoods.setRedeemedQuantity(goodsPO.getRedeemedQuantity() + 1);
        goodsService.updateById(updateGoods);
        return orderInsertVO;
    }

    /**
     * 订单详情
     *
     * @param id 订单id
     * @return
     */
    @Override
    public OrdersVO getOrderById(Integer id) {
        OrdersPO ordersPO = getById(id);
        OrdersVO vo = new OrdersVO();
        BeanUtils.copyProperties(ordersPO, vo);
        GoodsPO goodsPO = goodsService.getById(ordersPO.getId());
        vo.setGoodsNo(goodsPO.getGoodsNo());
        vo.setGoodsName(goodsPO.getGoodsName());
        vo.setGoodsDescription(goodsPO.getGoodsDescription());
        return vo;
    }

    /**
     * 查询订单列表
     *
     * @param dto 查询条件
     * @return 订单分页列表
     */
    @Override
    public IPage<OrdersVO> getOrders(OrdersDTO dto) {
        return this.ordersDAO.getOrders(dto);
    }

}

